package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService  categoryService;
    //保存套餐
    @PostMapping
    public R addSermeal(@RequestBody SetmealDto setmealDto){
       setmealService.addsave(setmealDto);
        return R.success("保存套餐成功");
    }
    //分页套餐方法
    @GetMapping("/page")
    public R selectAll(Integer page ,Integer pageSize,String name ){
        //分页对象中传入对象
        IPage<Setmeal> pages=new Page<>(page,pageSize);
        //因为有模糊查询要做个where判断
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        //因为可能不 模糊查询所以调用工具类判断一下
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //分页步骤1.想分页的对象调用page方法创造两个对象
        setmealService.page(pages,qw);
        //拿到条目数传给前端
        long total = pages.getTotal();
        //拿到页中对象集合
        List<Setmeal> records = pages.getRecords();
        //因为有口味所以要放入综合对象中
        List<SetmealDto>list=new ArrayList<>();
        //遍历 对象集合拿到每一个对象
        for (Setmeal setmeal : records) {
            //拿到每个对象的id
            Long setmealId = setmeal.getCategoryId();
            //根据套餐对象的id拿到每一个套餐对象
            Category category = categoryService.getById(setmealId);
            //根据套餐对象拿到套餐名
            String categoryName = category.getName();

            SetmealDto setmealDto=new SetmealDto();
            setmealDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(setmeal,setmealDto);
            //setmealDto.setSetmealDishes(records);
            list.add(setmealDto);
        }
        HashMap map=new HashMap();
        map.put("total",total);
        map.put("records",list);
        return R.success(map);
    }
    //删除套餐
    @DeleteMapping
    public R deleteSetmeal(@RequestParam List<Long>ids){
        int i = setmealService.deletes(ids);
        if(i<=0){
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }
    //修改套餐状态
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status
            ,@RequestParam List<Long>ids){
        setmealService.updateStatus(status,ids);
        return R.success("操作成功");
    }
    //回显修改数据
    @GetMapping("/{id}")
    //因为是路径传参所以这里用PathVariable
    public R selectSetmeal(@PathVariable Long id){
        SetmealDto setmealDto1= setmealService.selectSetmeal(id);
        return R.success(setmealDto1);
    }
    //修改套餐
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改套餐成功");
    }
    //前面只做了菜品套餐的展示还要套餐表的展示
    @GetMapping("/list")
    public R getSetmeal(Long categoryId,Integer status){
        //过滤条件
        QueryWrapper<Setmeal> qw=new QueryWrapper<>();
        qw.lambda().eq(Setmeal::getCategoryId,categoryId);
        qw.lambda().eq(Setmeal::getStatus,status);
        //套餐表集合
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }
}
